//
//  RoutineBloodViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/6/26.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "RoutineBloodViewController.h"
#import "SuifangSubitemViewCell.h"

@interface RoutineBloodViewController () <UITextFieldDelegate>
@property(nonatomic,strong) NSArray *rowUIArr;
@end

@implementation RoutineBloodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"血常规";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackGroundImage"]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存"
                                                                             style:UIBarButtonItemStyleDone
                                                                            target:self
                                                                            action:@selector(saveAction)];
    UIImage *img = [[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:img
                                                                            style:UIBarButtonItemStyleDone
                                                                           target:self
                                                                           action:@selector(backAction)];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.allowsSelection = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"SuifangSubitemViewCell"
                                               bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"cell"];
    self.rowUIArr = @[
                      @{@1:@"白细胞", @2:@"参考值:4-10x10^9/L", @3:@"10^9/L"},
                      @{@1:@"中性粒细胞数", @2:@"参考值:2-7x10^9/L", @3:@"10^9/L"},
                      @{@1:@"中性粒细胞比率", @2:@"参考值:50%-70%", @3:@"%"},
                      @{@1:@"淋巴细胞数", @2:@"参考值:0.8-4x10^9/L", @3:@"10^9/L"},
                      @{@1:@"单核细胞数", @2:@"参考值:0.12-0.8x10^9L", @3:@"10^9/L"},
                      @{@1:@"嗜酸性粒细胞数", @2:@"参考值:0.02-0.5x10^9/L", @3:@"10^9/L"},
                      @{@1:@"红细胞", @2:@"参考值:3.5-5.5x10^12/L", @3:@"10^12/L"},
                      @{@1:@"红细胞积压", @2:@"参考值:37%-50%", @3:@"%"},
                      @{@1:@"血红蛋白", @2:@"参考值:110-160 g/L", @3:@"g/L"},
                      @{@1:@"血小板", @2:@"参考值:100-300x10^9/L", @3:@"10^9/L"},
                      @{@1:@"淋巴细胞比率", @2:@"参考值:", @3:@"%"},
                      @{@1:@"嗜酸性粒细胞比率", @2:@"参考值:", @3:@"%"}
                      ];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 事件响应
-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)saveAction{
    for (int i=1; i<=self.rowUIArr.count; i++) {
        NSString *key = [NSString stringWithFormat:@"routineBlood%ld",(long)i];
        NSString *val = [self.routineBlood valueForKey:key];
        if (!val) {
            [self.routineBlood setValue:@"" forKey:key];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(backRoutinebloodData:)]) {
        [self.delegate backRoutinebloodData:self.routineBlood];
    }
    [self backAction];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SuifangSubitemViewCell staticHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rowUIArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *rowUIData = _rowUIArr[indexPath.row];
    SuifangSubitemViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SuifangSubitemViewCell alloc]init];
    }
    [cell renderUIWithText:rowUIData];
    cell.valTextfield.tag = indexPath.row+1;
    [cell.valTextfield addTarget:self action:@selector(textFieldDidChangeValue:) forControlEvents:UIControlEventEditingChanged];
    cell.valTextfield.text = [self.routineBlood valueForKey:[NSString stringWithFormat:@"routineBlood%ld",(long)indexPath.row+1]];
    return cell;
}

- (RPOutinebloodBean *)routineBlood{
    if (!_routineBlood) {
        _routineBlood = [RPOutinebloodBean new];
    }
    return _routineBlood;
}

#pragma mark UITextField代理
- (void)textFieldDidChangeValue:(id)sender{
    UITextField *textField = (UITextField*)sender;
    if (textField.text.length>0) {
        NSString *key = [NSString stringWithFormat:@"routineBlood%ld", (long)textField.tag];
        [_routineBlood setValue:textField.text forKey:key];
    }
}

@end
